﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;



using ConsoleApplication2.Models;
 
using DHTMLX.Scheduler;
using DHTMLX.Scheduler.Data;
using DHTMLX.Common;
using DHTMLX.Scheduler.Controls;
 
namespace ConsoleApplication2.Controllers
{
    public class BasicSchedulerController : Controller
    {
        public ActionResult Index()
        {

            var db = new HMS909Context();
            IEnumerable<SelectListItem> items = db.Doctors.ToList().Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.English + " - " + c.Arabic
            });
            ViewBag.doctors = items;
            items = db.rooms.ToList().Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.name
            });
            ViewBag.rooms = items;
            var scheduler = new DHXScheduler(this); //initializes dhtmlxScheduler
            scheduler.LoadData = true;// allows loading data
            scheduler.EnableDataprocessor = true;// enables DataProcessor in order to enable implementation CRUD operations
            var box = scheduler.Lightbox.SetExternalLightbox("BasicScheduler/LightboxControl", 420, 195);
            box.ClassName = "custom_lightbox";
            scheduler.InitialDate = new DateTime(2011, 9, 5);
            return View(scheduler);


        }
        public ActionResult LightboxControl(Event ev)
        {
            var context = new SampleDataContext();
            //var context = new DHXSchedulerModelsDataContext();
            var current = context.Events.SingleOrDefault(e => e.id == ev.id);
            if (current == null)
                current = ev;
            return View(current);
        }
        public ActionResult calendar()
        {

            var db = new HMS909Context();
            IEnumerable<SelectListItem> items = db.Doctors.ToList().Select(c => new SelectListItem
              {
                  Value = c.ID.ToString(),
                  Text = c.English + " - " +  c.Arabic 
              });
            ViewBag.doctors = items;
            items = db.rooms.ToList().Select(c => new SelectListItem
            {
                Value = c.id.ToString(),
                Text = c.name 
            });
            ViewBag.rooms = items;
            var scheduler = new DHXScheduler(this); //initializes dhtmlxScheduler
            scheduler.LoadData = true;// allows loading data
            scheduler.EnableDataprocessor = true;// enables DataProcessor in order to enable implementation CRUD operations
            var box = scheduler.Lightbox.SetExternalLightbox("BasicScheduler/LightboxControl", 420, 195);
            box.ClassName = "custom_lightbox";
            scheduler.InitialDate = new DateTime(2011, 9, 5);
            return View(scheduler);
        }

        public ActionResult Data()
        {//events for loading to scheduler
            return new SchedulerAjaxData(new SampleDataContext().Events);
        }
          public ActionResult Save(int? id, FormCollection actionValues)
        {
            var action = new DataAction(actionValues);
            var changedEvent = (Event)DHXEventsHelper.Bind(typeof(Event), actionValues);
            if (action.Type != DataActionTypes.Error)
            {
                //process resize, d'n'd operations...
                return NativeSave(changedEvent, actionValues);
            }
            else
            {
                //custom form operation
                return CustomSave(changedEvent, actionValues);
            }

        }

        public ActionResult CustomSave(Event changedEvent, FormCollection actionValues)
        {

            var action = new DataAction(DataActionTypes.Update, changedEvent.id, changedEvent.id);
            if (actionValues["actionButton"] != null)
            {
                SampleDataContext data = new SampleDataContext();
                try
                {
                    if (actionValues["actionButton"] == "Save")
                    {

                        if (data.Events.SingleOrDefault(ev => ev.id == action.SourceId) != null)
                        {
                            var eventToUpdate = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                            DHXEventsHelper.Update(eventToUpdate, changedEvent, new List<string>() { "id" });                           
                        }
                        else
                        {
                            action.Type = DataActionTypes.Insert;
                            data.Events.InsertOnSubmit(changedEvent);
                        }
                    }else if(actionValues["actionButton"] == "Delete"){
                        action.Type = DataActionTypes.Delete;
                        changedEvent = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                        data.Events.DeleteOnSubmit(changedEvent);
                    }                       
                    data.SubmitChanges();
                }

                catch
                {
                    action.Type = DataActionTypes.Error;
                }
            }
            else
            {
                action.Type = DataActionTypes.Error;
            }


            return (new SchedulerFormResponseScript(action, changedEvent));
           
        }

        public ContentResult NativeSave(Event changedEvent, FormCollection actionValues)
        {

            var action = new DataAction(actionValues);

            SampleDataContext data = new SampleDataContext();
            try
            {
                switch (action.Type)
                {
                    case DataActionTypes.Insert:
                        data.Events.InsertOnSubmit(changedEvent);
                        break;
                    case DataActionTypes.Delete:
                        changedEvent = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                        data.Events.DeleteOnSubmit(changedEvent);
                        break;
                    default:// "update"                          
                        var eventToUpdate = data.Events.SingleOrDefault(ev => ev.id == action.SourceId);
                        DHXEventsHelper.Update(eventToUpdate, changedEvent, new List<string>() { "id" });  
                        break;
                }
                data.SubmitChanges();
                action.TargetId = changedEvent.id;
            }
            catch 
            {
                action.Type = DataActionTypes.Error;
            }

            return (new AjaxSaveResponse(action));
        }
        


    }
}
